﻿using System;
using System.Linq;
using System.Windows;
using VsxFactory.Modeling.Repository;
using Microsoft.VisualStudio.ExtensionsExplorer;
using VsxFactory.Modeling.StrategyManager.Strategies;

namespace VsxFactory.Modeling.StrategyManager.Dialog.Providers
{
    /// <summary>
    /// IVsExtensionsProvider implementation responsible for gathering
    /// a list of installed packages which will be shown in the Add Package dialog.
    /// </summary>
    internal class SolutionPackagesProvider : PackagesProviderBase
    {
        public SolutionPackagesProvider(VisualStudioStrategyContext context, ResourceDictionary resources)
            : base(context, resources) {
        }

        public override string Name {
            get {
                return "Solution";
            }
        }

        public override bool RefreshOnNodeSelection {
            get {
                return true;
            }
        }

        public override IVsExtensionsTreeNode ExtensionsTree {
            get {
                RootNode = new RootPackagesTreeNode(null, String.Empty);
                CreateExtensionsTree();

                return RootNode;
            }
        }

        protected override void FillRootNodes() {
            var allNode = new SimpleTreeNode(this, "All", RootNode, Context.SolutionRepository);

            RootNode.Nodes.Add(allNode);
        }

        public override bool CanExecute(PackageItem item) {
            // enable command on a Package in the Installed provider if the package is installed.
            return Context.LocalRepository.GetPackages().Any(p => p.ID == item.PackageIdentity.ID && p.Version == item.Version);
        }

        protected override bool ExecuteCore(PackageItem item) {
            // Désactivation
            System.Diagnostics.Contracts.Contract.Requires(item != null);
            Context.SolutionRepository.Uninstall(item.PackageIdentity);

            //// Remove the item from the "All" tree of installed packages
            //var allTree = this.ExtensionsTree.Nodes.FirstOrDefault();
            //var uninstalledItem = allTree.Extensions.FirstOrDefault(c => c.Id.Equals(item.Id, StringComparison.OrdinalIgnoreCase));
            //if (uninstalledItem != null)
            //{
            //    allTree.Extensions.Remove(uninstalledItem);
            //}
            return true;
        }

        protected override void OnExecuteCompleted(PackageItem item) {
            if (SelectedNode != null) {
                SelectedNode.Extensions.Remove((IVsExtension)item);
            }
        }

        public override IVsExtension CreateExtension(PackageInfo package) {
            return new PackageItem(this, package, null) {
                CommandName = "Disable"
            };
        }
    }
    //internal class SolutionPackagesProvider : PackagesProviderBase {

    //    private ResourceDictionary _resources;
    //    private IPackageRepository _solutionRepository;

    //    public SolutionPackagesProvider(IPackageRepository solutionRepository, IPackageRepository localRepository, ResourceDictionary resources)
    //        : base(solutionRepository, localRepository, resources, false)
    //    {
    //        System.Diagnostics.Contracts.Contract.Requires(solutionRepository != null);
    //        System.Diagnostics.Contracts.Contract.Requires(resources != null);
    //        _resources = resources;
    //        _solutionRepository = solutionRepository;
    //    }

    //    public override void Install(VsxFactory.Modeling.Repository.PackageInfo package)
    //    {
    //        throw new NotImplementedException();
    //    }

    //    public override void Uninstall(PackageInfo package)
    //    {
    //        // Désactivation
    //        System.Diagnostics.Contracts.Contract.Requires(package != null);
    //        _solutionRepository.Uninstall(package);

    //        // Remove the item from the "All" tree of installed packages
    //        var allTree = this.ExtensionsTree.Nodes.FirstOrDefault();
    //        var uninstalledItem = allTree.Extensions.FirstOrDefault(c => c.Id.Equals(package.ID, StringComparison.OrdinalIgnoreCase));
    //        if (uninstalledItem != null)
    //        {
    //            allTree.Extensions.Remove(uninstalledItem);
    //        }
    //    }

    //    public override IQueryable<PackageInfo> GetQuery()
    //    {
    //        return from p in base.GetQuery()
    //               from p2 in _localRepository.GetPackages()
    //               where p.ID == p2.ID
    //               select p2;
    //    }

    //    public override bool IsInstalled(PackageInfo package)
    //    {
    //        return true; // Toujours
    //    }

    //    internal override bool CanBeActivate(PackageInfo package)
    //    {
    //        return true;
    //    }

    //    public override string Name {
    //        get {
    //            return "Activated";
    //        }
    //    }

    //    private object _mediumIconDataTemplate;
    //    public override object MediumIconDataTemplate {
    //        get {
    //            if (_mediumIconDataTemplate == null) {
    //                _mediumIconDataTemplate = _resources["OnlineUpdateTileTemplate"];
    //            }
    //            return _mediumIconDataTemplate;
    //        }
    //    }

    //    internal void Refresh()
    //    {
    //        this.RootNode = null;
    //    }
    //}
}